package com.guigu.utils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.guigu.utils.bean.ExcelPropertyIndexModel;

public class ExcelWriteWithExcelProperty {

	public static void main(String[] args) {
		try {
			writeWithHead();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Test
	public static void writeWithHead() throws IOException {
		String inputPath = "D:\\tmp\\excel\\withExcelProperty.xlsx";
		try (OutputStream out = new FileOutputStream(inputPath);) {
			ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
			Sheet sheet1 = new Sheet(1, 0, ExcelPropertyIndexModel.class);
			sheet1.setSheetName("sheet1");
			List<ExcelPropertyIndexModel> data = new ArrayList<>();
			for (int i = 0; i < 100; i++) {
				ExcelPropertyIndexModel item = new ExcelPropertyIndexModel();
				item.setName("name" + i);
				item.setAge("age" + i);
				item.setEmail("email" + i);
				if (i != 5) {
					item.setAddress("address" + i);
				}
//				item.setAddress("address" + i);
				item.setSax("sax" + i);
				item.setHeigh("heigh" + i);
				item.setLast("last" + i);
				data.add(item);
			}
			writer.write(data, sheet1);
			writer.finish();
		}
	}

}
